package code45;

import java.util.Scanner;

public class RunLittleSupperMarketAppMain {
    public static void main(String[] args) {

        // 创建一个小超市类
        LittleSuperMarket littleSuperMarket = new LittleSuperMarket();
        // 依次给超市的名字、地址、停车位赋值
        littleSuperMarket.superMarketName = "有家超市";
        littleSuperMarket.address = "世纪大道666号";
        littleSuperMarket.parkingCount = 200;
        // 给超市200种商品
        littleSuperMarket.merchandises = new Merchandise[200];
        // 统计用的数组
        littleSuperMarket.merchandiseSold = new int[littleSuperMarket.merchandises.length];

        // 为使用方便，创建一个商品数组引用，和littleSuperMarket.merchandises指向同一个数组对象
        Merchandise[] all = littleSuperMarket.merchandises;

        // 遍历并给200中商品赋值
        for (int i = 0; i < all.length; i++) {
            // 创建并给商品的属性赋值
            Merchandise m = new Merchandise();
            m.count = 200;
            m.id = "ID" + i;
            m.name = "商品" + i;
            m.purchasePrice = Math.random() * 200;
            m.soldPrice = (1 + Math.random()) * 200;
            all[i] = m;
        }

        System.out.println("下面请利润最高的商品自我介绍：");
        // >> TODO 返回值可以直接使用，不必赋值给一个变量再使用
        littleSuperMarket.getBiggestProfitMerchandise().describe();

        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("今日超市大特惠，所有商品第二件半价！请选择要购买的商品索引：");
            int index = scanner.nextInt();

            if (index < 0) {
                System.out.println("欢迎再次光临！");
                break;
            }

            if (index >= all.length) {
                System.out.println("商品索引超出界限");
                continue;
            }

            Merchandise m = all[index];
            Merchandise m0 = all[0];

            System.out.println("商品" + m.name + "售价为" + m.soldPrice + "。请问购买几个？");
            int numToBuy = scanner.nextInt();

            System.out.println("用户选择的商品是超市里价值最高的：" +
                    m.isTheBiggestTotalValueOne(littleSuperMarket));

//            double totalCost = m.buy(m.count);
            double totalCost = m.buyAndPrintLeft(m.count, true);

            boolean m0BiggerThan = m0.totalValueBiggerThan(m);
            System.out.println("m0的总价值比用户选择的要大：" + m0BiggerThan);

            System.out.println("选购的商品总价为" + totalCost);
        }

    }
}
